<?php

class ProductsModel {

	private $_db;
	private $_pam;

    public function __construct() {
		$this->_db = DBFactory::getDbConnection();
		$this->_pam = new ProductsAddsModel();
    }

    public function get($id) {
        $id = (int) $id;
        $q = $this->_db->prepare("SELECT id, display, date, title, text, adds FROM products WHERE id=:id");
		$q->execute(array('id'=>$id));
        $data = $q->fetch(PDO::FETCH_ASSOC);
        return new ProductsObject($data, $this->_pam->getList(), $this->getAddsList($data['adds']));
    }

    public function getList($type) {
		switch ($type) {
			case 'full':
				$type = '';
				break;
			case 'display':
				$type = 'WHERE display=1';
				break;
			case 'notdisplay':
				$type = 'WHERE display=0';
				break;
		}
        $q = $this->_db->prepare("SELECT id, display, title, date, text, link, adds FROM products $type ORDER BY id DESC");
		$q->execute();
        $tproducts = array();
        while ($data = $q->fetch(PDO::FETCH_ASSOC)) {
			$product = new ProductsObject($data, $this->_pam->getList());
			$product = $this->setAddsDisplay($data['adds'], $product);
            $tproducts[] = $product;
        }
		//Tools::dump($tproducts);
        return $tproducts;
    }
	
	public function setAddsDisplay($adds, $product) {
		$addsdisplayarray = explode(';',$adds);
		foreach ($product->adds() as $add) {
			if (!in_array($add->id(), $addsdisplayarray)) {
				$add->setDisplay(0);
			}
		}
		return $product;
	}

	public function getAddsList($adds) {
		$sql = explode(';',$adds);
		$tadds = array();
		foreach($sql as $sqlval){
			$q = $this->_db->prepare("SELECT id, title FROM products_adds ORDER BY id DESC");
			$q->bindparam('sqlval', $sqlval);
			$q->execute();
			while ($data = $q->fetch(DBFactory::assoc())) {
				$tadds['adds'][] = new ProductsAddsObject($data);
			}
		}
		return $tadds;
	}
	
	//CREATE OBJECT
	public function addObj() {
		$q = $this->_db->prepare("INSERT INTO products VALUES(null, '0', 'title', '0000-00-00', 'text', 'link', '')");
		$q->execute();
		return "1";
	}
	
	//DELETE OBJECT
	public function delObj($id) {
		$id = (int)$id;
		$q = $this->_db->prepare("DELETE FROM products WHERE id=:id");
		$q->execute(array('id'=>$id));
		$q->execute();
		//Delete images directory and directory itself
		Tools::removeDir(PATH_DATAS_PRODUCTS.$id.'/tb');
		Tools::removeDir(PATH_DATAS_PRODUCTS.$id);
		return "1";
	}
	
	//DELETE GROUP OF ITEMS SELECTED
	public function delGroup($group) {
		$group = json_decode($group);
		foreach ($group as $key) {
			$this->delObj($key);
		}
		return "1";
	}
	
	//CHECK IF STRING IS JSON
	public function isJson($string) {
		json_decode($string);
		if (json_last_error() == JSON_ERROR_NONE) {
			return true;
		}
	}
	
	//UPDATE OBJECT
	public function upObj($data) {
		if (!is_array($data)) {
			$data = json_decode($data);
		}
		$display = 0;
		$adds = '';
		foreach ($data as $key) {
			$name = $key->name;
			$value = $key->value;
			switch ($name) {
				case 'id':
					$id = $value;
					break;
				case 'display':
					$display = $value;
					break;
				case 'title':
					$title = $value;
					break;
				case 'date':
					$date = $value;
					break;
				case 'text':
					$text = $value;
					break;
				case 'link':
					$link = $value;
					break;
				case 'adds':
					$adds = $adds.$value.';';
					break;
			}
		}
		$q = $this->_db->prepare("UPDATE products SET display=:display, title=:title, date=:date, text=:text, link=:link, adds=:adds WHERE id=:id");
		$q->execute(array(
			'id'=>$id,
			'display'=>$display,
			'title'=>$title,
			'date'=>$date,
			'text'=>$text,
			'link'=>$link,
			'adds'=>$adds
		));
		return "1";
	}
	
	public function upAll($dataArray) {
		$dataArray = json_decode($dataArray);
		foreach ($dataArray as $key) {
			$this->upObj($key);
		}
		return "1";
	}
	
	//UPLOAD FILE
	public function upload($id, $dir, $filename, $content) {
		$upload = new Upload();
		$upload->dadUpload($id, $filename, $content, PATH_DATAS.$dir, true);
	}

}

?>